package com.changgou.config.controller;

import com.changgou.config.pojo.FreightTemplate;
import com.changgou.config.service.FreightTemplateService;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/freight")
public class FreightTemplateController {
    @Autowired
    private FreightTemplateService freightTemplateService;

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable int id){
        freightTemplateService.delete(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    /**
     * 添加运费模板
     * @return
     * fxq
     */
    @PostMapping
    public Result add(@RequestBody FreightTemplate freightTemplate){
        //调用方法
        try {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format(date);
            //设置添加时间
            freightTemplate.setAddtime(dateStr);
            //设置是否启用,默认启用
            freightTemplate.setUseable(1);
            freightTemplateService.add(freightTemplate);
            return new Result(true,StatusCode.OK,"添加运费模板成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加运费模板失败");
        }
    }

    /**
     * 分页查询模板列表
     * fxq
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result findAll(@PathVariable("page") int page, @PathVariable("size") int size){
        try {
            PageResult<FreightTemplate> list = freightTemplateService.findAll(page,size);
            return new Result(true,StatusCode.OK,"查询列表成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询列表失败");
        }
    }

    /**
     * 运费模板下拉框查询
     * zxq
     * @return
     */
    @GetMapping("/findList")
    public Result findList(){
        try {
            List<FreightTemplate> list = freightTemplateService.findList();
            return new Result(true,StatusCode.OK,"查询模板成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询模板失败");
        }
    }
}
